package com.pft.web.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;


/**
 * Create by Chenstyle on 2017-4-19
 */

public class CustomViewFinder extends View {

    private static final int BACK_RECT_COLOR = 0x99000000; // 背景白色矩形框
    private static final int REC_COLOR = 0xFF2267c3; // 扫描框四个蓝色角
    private static final int WHITE_COLOR = 0xFFFFFFFF; // 扫描框四个蓝色角
    private static final int LINE_COLOR = 0x0000FF00; //
    private Paint mBackPaint = new Paint(); // 用于绘制背景的白色矩形框
    private Paint mAngelPaint = new Paint(); // 用于绘制四个矩形角的直角
    private TextPaint textPaint = new TextPaint(); // 用于绘制中间的动画闪光条
    private int mWidth = 0;
    private int mHeight = 0;


    private int lineHeight;
    private int lineWidth;

    private int marginTop;//距离顶部高度
    private int scanFrameWidth;//扫描框 宽度


    private String tipContent;
    private float density;

    /**
     * 扫描框距离 顶部距离
     *
     * @param marginTop
     */
    public void setMarginTop(int marginTop) {
        this.marginTop = marginTop;
    }

    /**
     * 扫描框大小
     *
     * @param scanFrameWidth
     */
    public void setScanFrameWidth(int scanFrameWidth) {
        this.scanFrameWidth = scanFrameWidth;
    }


    /**
     * 提示内容
     *
     * @param tipContent
     */
    public void setTipContent(String tipContent) {
        this.tipContent = tipContent;
    }

    public CustomViewFinder(Context context) {
        super(context);
    }

    public CustomViewFinder(Context context, AttributeSet attrs) {
        super(context, attrs);

        density = getResources().getDisplayMetrics().density;
        mBackPaint.setColor(BACK_RECT_COLOR);
        mBackPaint.setStyle(Paint.Style.FILL);

        mAngelPaint.setColor(REC_COLOR);
        mAngelPaint.setStyle(Paint.Style.FILL);

        textPaint.setColor(WHITE_COLOR);
        textPaint.setStyle(Paint.Style.FILL);


        textPaint.setTextSize(17 * density);

        tipContent = null;

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = MeasureSpec.getSize(widthMeasureSpec);
        mHeight = MeasureSpec.getSize(heightMeasureSpec);

        marginTop = (int) (100 * getResources().getDisplayMetrics().density);
        scanFrameWidth = (int) (mWidth * 0.7);

        lineHeight = (int) (4 * getResources().getDisplayMetrics().density);
        lineWidth = (int) (30 * getResources().getDisplayMetrics().density);
    }


    public Rect getFrameRect() {
        Rect rect = new Rect();
        rect.left = (mWidth - scanFrameWidth) / 2;
        rect.right = rect.left + scanFrameWidth;
        rect.top = marginTop;
        rect.bottom = marginTop + scanFrameWidth;
        return rect;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // 测量得出扫描框宽度 高度等于宽度 宽度大小为屏幕的 70%
        // 绘制白色矩形扫描框 宽度 1px 转化成1dp用 考虑高分屏

        Rect rect = getFrameRect();

        //  canvas.drawRect(rect, mBackPaint);


        canvas.drawRect(0, 0, rect.left, mHeight, mBackPaint);
        canvas.drawRect(rect.left, 0, mWidth, rect.top, mBackPaint);
//
        canvas.drawRect(rect.right, rect.top, mWidth, mHeight, mBackPaint);
//
        canvas.drawRect(rect.left, rect.bottom, rect.right, mHeight, mBackPaint);


        // 绘制四个角 宽度 6px 长度 32px 转化为 宽 3dp 长 16dp 用

//
//        // 绘制左上角横指示
        canvas.drawRect(rect.left, rect.top, rect.left + lineWidth, rect.top + lineHeight, mAngelPaint);
//        // 绘制左上角竖指示
        canvas.drawRect(rect.left, rect.top, rect.left + lineHeight, rect.top + lineWidth, mAngelPaint);
//
//        // 绘制右上角横指示
        canvas.drawRect(rect.right - lineWidth, rect.top, rect.right, rect.top + lineHeight, mAngelPaint);
        // 绘制右上角竖指示
        canvas.drawRect(rect.right - lineHeight, rect.top, rect.right, rect.top + lineWidth, mAngelPaint);
//
////        // 绘制左下角横指示
        canvas.drawRect(rect.left, rect.bottom - lineHeight, rect.left + lineWidth, rect.bottom, mAngelPaint);
        // 绘制左下角竖指示
        canvas.drawRect(rect.left, rect.bottom - lineWidth, rect.left + lineHeight, rect.bottom, mAngelPaint);

//        // 绘制右下角横指示
        canvas.drawRect(rect.right - lineWidth, rect.bottom - lineHeight, rect.right, rect.bottom, mAngelPaint);
        // 绘制右下角竖指示
        canvas.drawRect(rect.right - lineHeight, rect.bottom - lineWidth, rect.right, rect.bottom, mAngelPaint);


        if (!TextUtils.isEmpty(tipContent)) {
            StaticLayout staticLayout = new StaticLayout(tipContent, textPaint, scanFrameWidth, Layout.Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);
            canvas.translate(rect.left, rect.bottom + 15 * density);
            staticLayout.draw(canvas);
        }


    }
}
